#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>

#include <linux/input.h>

#define KEY_EVENT "/dev/input/event1"  //cat /proc/bus/input/devices

int main(int argc, char **argv) {
	int fd;
	struct input_event ie;

	fd = open(KEY_EVENT, O_RDONLY);
	if (fd == -1) {
		fprintf(stderr, "Cannot open %s: %s\n", KEY_EVENT, strerror(errno));
		return EXIT_FAILURE;
	}

	printf("Press any key on the keyboard...\n");


	//inject key event:
	//  Qemu: sendkey ctrl-shift-3
	//  Virsh: virsh send-key my-vm KEY_LEFTCTRL KEY_SHIFT KEY_3
	while (read(fd, &ie, sizeof(ie)) == sizeof(ie)) {
		if (ie.type == EV_KEY && ie.value == 1) { // 1 表示按键按下
			printf("Key %d pressed\n", ie.code);
		}
	}


	close(fd);

	return EXIT_SUCCESS;
}
